import time

from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
class Base():
    def __init__(self,driver):
        self.driver=driver
    def open_url(self,url):
        self.driver.get(url)

    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            str=time.strftime('%Y%m%d%H%m%s')
            self.driver.save_screenshot(str,'jietu.png')
            print(e)
    def send_key(self,loc,info):
        self.find_element(loc).send_key(info)

    def click(self,loc):
        self.find_element(loc).click()

    def get_text(self,loc):
        return self.find_element(loc).text()


    # 滚动指定的方法
    def scroll_to_element(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')
    def scroll_buttom(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollHeight())')
    #     封装多页面切换的方法
    def switch_to(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))
    #     退出frame框架
    def disframe(self):
        self.driver.switch_to.default_content()
    #     弹窗
    def alter(self):
        self.driver.switch_to.alert.accept()
    #    下拉框
    def select_get_value(self,loc,value):
        Select(self.find_element(loc)).select_by_value(value)
    def select_get_text(self,loc,text):
        Select(self.find_element(loc)).select_by_visible_text(text)
    def select_get_index(self,loc,index):
        Select(self.find_element(loc)).select_by_index(index)
#     鼠标悬停
    def move_to_element(self,loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()
#     鼠标右击
    def context_click(self,loc):
        ActionChains(self.driver).context_click(self.find_element(loc)).perform()
    def double_clikc(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()
    #键盘回车
    def keys_enter(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)
        self.driver.find_element().send_keys(Keys.CONTROL, 'a')
    # 键盘全选
    def keys_all(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'a')
    # 键盘复制
    def keys_c(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'c')
    # 键盘粘贴
    def keys_v(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'v')
    # 浏览器事件
    # 退格
    def back(self):
        self.driver.back()
    #刷新
    def refresh(self):
        self.driver.refresh()









